import { defineStore } from "pinia";
import { ref } from "vue";
import { roleGetApi, userLoginApi } from "../api/user";
import router from "../router";

export const useAccount = defineStore('account',()=>{
  // 用户数据
  const userInfo = ref(null)
  const jwtToken = ref('')
  const permitData = ref(null)
  // 登录
  const loginAccount = async (payload)=>{
    console.log('2222',payload);
    
    let res = await userLoginApi(payload)
    console.log(res);
    let {code,token,data} = res.data
    if(code==1){  //登录成功
      userInfo.value = data
      jwtToken.value = token
      //使用roleId获取permission
      let permit = await roleGetApi(data.roleId)
      console.log('角色对应的权限信息',permit);
      permitData.value = permit.data.data[0]
      router.push('/')
    }
  }
  return {
    userInfo,
    jwtToken,
    permitData,
    loginAccount
  }
},{
  persist:{
    key:'mvvm-account-01'
  }
})